﻿require.config({
	urlArgs: "version=20170407",
    paths: {
        'jquery': 'packages/jquery',
        'bootstrap': '../bootstrap/js/bootstrap.min'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'bootstrap'
        }
    }
});

require(['jquery', 'bootstrap'],function($){
	
	$('#login-submit').click(function () {
		login();
	  });
	var verifable=true;
	$('#get-verif').click(function () {
		 if(verifable){
			 verifable=false;
			 var clock = '';
			 var nums = 30;

			 var btn = document.getElementById("get-verif");
			 
			 btn.innerHTML = nums+'秒';
			 clock = setInterval(doLoop, 1000); //一秒执行一次
			 function doLoop()
			 {
				 nums--;
				 if(nums > 0){
					 	btn.innerHTML = nums+'秒';
				 }else{
					 clearInterval(clock); //清除js定时器
					 btn.disabled = false;
					 btn.innerHTML = '验证码';
					 nums = 30; //重置时间
					 verifable=true;
				 }

			 }
			
			sendCode(); 
		 }
		 
	  }); 
	$('#regist-submit').click(function () {
		register();
	  });
	$('#comregist-submit').click(function () {
		comregister();
	  });
	$('#findpass-submit').click(function () {
		findPass();
	  });
	
	function login() {
		  var username=document.getElementById('loginusername').value;
		  var password=document.getElementById('loginpassword').value;
		  alert(username+password);
		  $.ajax({
		    type: 'POST',
		    url: '/login',
		    cache :false,
		    data:{
		    	"account":username,
		    	"password":password,
		    },
		    success: function (res) {
		      if (res.status == 200) {
		    	window.location.href = "admin/selfadmin.html";
		      } else if (res.status == 403){
		        alert("密码错误！");
		      }else if (res.status == 404){
		    	  alert("用户不存在！"); 
		      }
		      else if (res.status == 402){
		    	  alert("用户被禁用!！"); 
		      }
		    },
		    error: function (xml, err, throwInfo) {
		      alert(err+"+"+throwInfo);
		      window.location.href = "/user/selfmlib.html";
		    }
		  });
	  }
	  
	  function sendCode() {
		  var registerusername=document.getElementById('registerusername').value;
		 
		  $.ajax({
			    type: 'POST',
			    url: '/sendMsg',
			    cache :false,
			    data:{
			    	"telenumber":registerusername,
			    },
			    success: function (res) {
			      if (res.status == 200) {
			    	  alert("验证码发送完成！！");
			      }else if (res.status == 404){
			        alert("验证码发送失败！！");
			      }
			    },
			    error: function (xml, err, throwInfo) {
			      alert(err);
			    }
			  });
	  }
	  
	  function findPass() {
		  var registerusername=document.getElementById('registerusername').value;
		  var content=document.getElementById('code').value;
		  
		  $.ajax({
			    type: 'POST',
			    url: '/users/findpass',
			    cache :false,
			    data:{
			    	"account":registerusername,
			    	"content":content
			    },
			    success: function (res) {
			      if (res.status == 200) {
			       alert("找回成功，你的新密码已发送至您的手机！");
			      }else if (res.status == 403){
				      alert("验证码错误！");
				  }
			    },
			    error: function (xml, err, throwInfo) {
			      alert(err);
			    }
			  });
	  }
	  
	  function register() {
		  var registerusername=document.getElementById('registerusername').value;
		  var registerpassowrd=document.getElementById('registerpassowrd').value;
		  var com_id=document.getElementById('com_id').value;
		  var content=document.getElementById('code').value;
		  alert(registerusername+"--"+registerpassowrd+"=="+content);
		  alert(com_id);
		  $.ajax({
		    type: 'POST',
		    url: '/users',
		    cache :false,
		    data:{
		    	"account":registerusername,
		    	"password":registerpassowrd,
		    	"com_id":com_id,
		    	"content":content
		    },
		    success: function (res) {
		      if (res.status == 200) {
		       alert(registerusername+"注册成功"+registerpassowrd);
		       window.location.href = "/admin/selfadmin.html";
		      } else if (res.status == 409){
		    	  alert("该电话已被注册！");
		      } else if (res.status == 403){
			      alert("验证码错误！");
			  } else if (res.status == 404){
			      alert("公司不存在！");
			  }
		    },
		    error: function (xml, err, throwInfo) {
		      alert(err);
		    }
		  });
	  }
	  function comregister() {
		  var registerusername=document.getElementById('registerusername').value;
		  var registercomname=document.getElementById('registercomname').value;
		  var registerpassowrd=document.getElementById('registerpassowrd').value;
		  var content=document.getElementById('code').value;
		  alert(registerusername+"--"+registerpassowrd+"=="+content);
		  $.ajax({
		    type: 'POST',
		    url: '/users/com',
		    cache :false,
		    data:{
		    	"account":registerusername,
		    	"password":registerpassowrd,
		    	"comname":registercomname,
		    	"content":content
		    },
		    success: function (res) {
		      if (res.status == 200) {
		       alert(registercomname+"公司注册成功等待审核");
		      } else if (res.status == 409){
		    	  alert("该电话已被注册！");
		      } else if (res.status == 403){
			      alert("验证码错误！");
			  } else if (res.status == 405){
			      alert("公司已被注册！");
			  }
		    },
		    error: function (xml, err, throwInfo) {
		      alert(err);
		    }
		  });
	  }
});
